package com.kma.summer2012.server;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.kma.summer2012.client.CrumbsUserService;
import com.kma.summer2012.dao.CrumbsUserDAO;
import com.kma.summer2012.exceptions.CrumbsUniversalException;
import com.kma.summer2012.factory.DAOFactory;
import com.kma.summer2012.factory.SpringBeansFactory;
import com.kma.summer2012.model.CrumbsUser;
import com.kma.summer2012.server.proxy.Proxy;
import com.kma.summer2012.shared.model.UICrumbsUser;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Администратор
 * Date: 12.01.13
 * Time: 23:42
 * To change this template use File | Settings | File Templates.
 */
public class CrumbsUserServiceImpl extends RemoteServiceServlet implements CrumbsUserService {


    public UICrumbsUser getById(int id) {
        DAOFactory factory = (DAOFactory) SpringBeansFactory.getInstance().getBeanByClass(DAOFactory.class);
        try {
            return Proxy.getInstance().createFromUser((CrumbsUser) factory.getCrumbsUserDAO().getById(id));
        } catch (SQLException e) {
            throw new CrumbsUniversalException("Error in connection to database",e);  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    public List<UICrumbsUser> getAllUsers() {
        List<UICrumbsUser> uiCrumbsUsers = new ArrayList<UICrumbsUser>();
        DAOFactory factory = (DAOFactory) SpringBeansFactory.getInstance().getBeanByName("daoFactory");
        CrumbsUserDAO dao= (CrumbsUserDAO)factory.getCrumbsUserDAO();
        try {
            for (CrumbsUser user : dao.getAll()) {
                uiCrumbsUsers.add(Proxy.getInstance().createFromUser(user));
            }
        } catch (SQLException e) {
            throw new CrumbsUniversalException("Error in connection to database",e);  //To change body of catch statement use File | Settings | File Templates.
        }
        return uiCrumbsUsers;
    }

}
